﻿using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;
using BitcoinMarket.Resources;
using System.Collections.ObjectModel;

namespace BitcoinMarket
{
    public class NetworkStatusChartSettingsItem
    {
        public string keyName { get; set; }
        public bool enabledDefault { get; set; }
        public string publicUrl { get; set; }
        public string localUrl { get; set; }
        public string chartTitle { get; set; }
        public string chartYAxisLabel { get; set; }
        public string chartDescription { get; set; }
    }

    public class AppNetworkStatusChartSettings
    {
        // Our settings
        IsolatedStorageSettings settings;

        //collection of items to show
        Collection<NetworkStatusChartSettingsItem> chartItems = null;

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public AppNetworkStatusChartSettings()
        {
            //Create the list of available settings
            this.chartItems = new Collection<NetworkStatusChartSettingsItem>();

            this.chartItems.Add(new NetworkStatusChartSettingsItem { keyName = "charts_HashRateSetting", enabledDefault = true, chartDescription = AppResources.HashRateChartDescription, chartTitle = AppResources.HashRateChartTitle, chartYAxisLabel = AppResources.HashRateChartYAxisTitle, localUrl = "http://localhost/hash-rate.json", publicUrl = "http://blockchain.info/charts/hash-rate?timespan=1year&daysAverageString=7&scale=0&format=json" });
            this.chartItems.Add(new NetworkStatusChartSettingsItem { keyName = "charts_MarketsPriceSetting", enabledDefault = true, chartDescription = AppResources.MarketsPriceChartDescription, chartTitle = AppResources.MarketsPriceChartTitle, chartYAxisLabel = AppResources.MarketsPriceChartYAxisTitle, localUrl = "http://localhost/market-price.json", publicUrl = "http://blockchain.info/charts/market-price?timespan=1year&daysAverageString=7&scale=0&format=json" });
            this.chartItems.Add(new NetworkStatusChartSettingsItem { keyName = "charts_AvgConfirmSetting", enabledDefault = true, chartDescription = AppResources.AvgConfirmTimeChartDescription, chartTitle = AppResources.AvgConfirmTimeChartTitle, chartYAxisLabel = AppResources.AvgConfirmTimeChartYAxisTitle, localUrl = "http://localhost/avg-confirmation-time.json", publicUrl = "http://blockchain.info/charts/avg-confirmation-time?timespan=1year&daysAverageString=7&scale=0&format=json" });

            this.chartItems.Add(new NetworkStatusChartSettingsItem { keyName = "charts_MinersRevenueSetting", enabledDefault = false, chartDescription = AppResources.MinersRevenueChartDescription, chartTitle = AppResources.MinersRevenueChartTitle, chartYAxisLabel = AppResources.MinersRevenueChartYAxisTitle, localUrl = "http://blockchain.info/charts/miners-revenue?timespan=1year&daysAverageString=7&scale=0&format=json", publicUrl = "http://blockchain.info/charts/miners-revenue?timespan=1year&daysAverageString=7&scale=0&format=json" });
            this.chartItems.Add(new NetworkStatusChartSettingsItem { keyName = "charts_CostPerTransactionSetting", enabledDefault = false, chartDescription = AppResources.CostPerTransactionChartDescription, chartTitle = AppResources.CostPerTransactionChartTitle, chartYAxisLabel = AppResources.CostPerTransactionChartYAxisTitle, localUrl = "http://blockchain.info/charts/cost-per-transaction?timespan=1year&daysAverageString=7&scale=0&format=json", publicUrl = "http://blockchain.info/charts/cost-per-transaction?timespan=1year&daysAverageString=7&scale=0&format=json" });

            // Get the settings for this application.
            settings = IsolatedStorageSettings.ApplicationSettings;
        }

        /// <summary>
        /// Returns the list of available network info chart items
        /// </summary>
        /// <returns></returns>
        public Collection<NetworkStatusChartSettingsItem> GetAllNetworkInfoItems()
        {
            return this.chartItems;
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;
            Key = "ntw_" + Key;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }

            Save();

            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            T value;
            Key = "ntw_" + Key;

            // If the key exists, retrieve the value.
            if (settings.Contains(Key))
            {
                value = (T)settings[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            settings.Save();

            // Reload the charts
            App.MainPageObj.loadNetworkData();
        }
    }
}
